# _*_ coding: utf-8 _*_
# @Time   : 2025/5/14 08:40
# @Author : 云舒
# @File   : 爬取爱奇艺视频信息.py

import pymongo
import threading
import requests
from queue import Queue
import time

class AiqiyiSpider:
    """
    爱奇艺视频分类信息爬虫
    热播榜单--内地
    """
    def __init__(self):
        self.client = pymongo.MongoClient(host='localhost', port=27017)
        self.db = self.client['spyder']['aiqiyi']
        self.api_url = 'https://pcw-api.iqiyi.com/search/recommend/list?channel_id=2&data_type=1&mode=11&page_id={}&ret_num=48&session=85dd981b69cead4b60f6d980438a5664&three_category_id=15;must'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        self.url_queue = Queue()  # 请求地址队列
        self.json_queue = Queue()  # json数据队列
        self.content_dict_queue = Queue()  # 内容字典队列

    def get_movie_url(self, page):
        url = self.api_url.format(page)
        self.url_queue.put(url)


    def get_movie_json(self):
        while True:
            url = self.url_queue.get()
            response = requests.get(url, headers=self.headers).json()
            self.json_queue.put(response)
            self.url_queue.task_done()

    def get_movie_info(self):
        while True:
            response = self.json_queue.get()
            movie_list = response['data']['list']
            for movie in movie_list:
                movie_info = dict()
                movie_info['name'] = movie['name']
                movie_info['description'] = movie['description']
                movie_info['period'] = movie['period']
                movie_info['main_charactor'] = movie['people']['main_charactor']
                movie_info['categories'] = movie['categories']
                movie_info['playUrl'] = movie['playUrl']
                self.content_dict_queue.put(movie_info)
            self.json_queue.task_done()

    def save_movie_info(self):
        while True:
            movie_info = self.content_dict_queue.get()
            self.db.insert_one(movie_info)
            print('插入成功')
            self.content_dict_queue.task_done()


    def main(self):
        thread_obj_list = list()
        for page in range(1, 3):
            thread_obj_list.append(threading.Thread(target=self.get_movie_url, args=(page,)))
        for _ in range(3):
            thread_obj_list.append(threading.Thread(target=self.get_movie_json))
        thread_obj_list.append(threading.Thread(target=self.get_movie_info))
        thread_obj_list.append(threading.Thread(target=self.save_movie_info))

        for thread in thread_obj_list:
            thread.daemon = True
            thread.start()

        time.sleep(3)

        for queue in [self.url_queue,  self.json_queue, self.content_dict_queue]:
            queue.join()

        print('任务完成')


if __name__ == '__main__':
    spider = AiqiyiSpider()
    spider.main()
